package com.pinc.cpbl;

import java.io.BufferedInputStream;
import java.io.InputStream;

public class Parser_Grade extends MyHTMLparser {
	private static final int SHOW_COUNT=10;
	private String[] items;
	private int qfield;

	public Parser_Grade(InputStream istream, int i_qfield) {
		super(istream, "utf8");
		items = new String[Grade_Adapter.ITEMS];
		qfield = i_qfield;
	}

	public void setInputStream(InputStream istream)
	{
		stream = new BufferedInputStream(istream, 1024);
	}

	public boolean parsePlayerInfo(Grade_Adapter adapter)
	{
		String retStr;
		String tmpStr;
		int loop_index = 0;
		int arr_index = 0;
		int show_count = SHOW_COUNT;

		if ( !checkTable())
			return false;

		nextTag();
		retStr = getTagName();

		while( retStr.compareTo("/table") != 0 && show_count >= 0)
		{
			if ( retStr.compareTo("tr") == 0)
			{				
				if ( arr_index > 0)
				{
					adapter.add(items);
					show_count--;
				}
				loop_index = 0;
				arr_index = 0;
			}
			else if( retStr.compareTo("td") == 0 || retStr.compareTo("th") == 0)
			{
				if ( loop_index <= 1)
				{
					if ( retStr.compareTo("td") == 0)
						tmpStr = getContent("td");
					else
						tmpStr = getContent("th");

					tmpStr = tmpStr.replaceAll("<[^<>]+>", "").trim();
					items[arr_index++] = tmpStr;
				}
				loop_index++;
			}
			nextTag();
			retStr = getTagName();
		}

		if ( arr_index > 0)
			adapter.add(items);

		return true;
	}

	public boolean parseQueryItem(Grade_Adapter adapter)
	{
		String retStr;
		String tmpStr="";
		int loop_index = 0;
		int arr_index = 0;
		int show_count = 0;

		if ( !checkTable())
			return false;

		nextTag();
		retStr = getTagName();

		while( retStr.compareTo("/table") != 0 && show_count <= SHOW_COUNT)
		{
			if ( retStr.compareTo("tr") == 0)
			{				
				if ( arr_index > 0)
				{
					adapter.update(show_count, 2, tmpStr);
					show_count++;
				}
				loop_index = 0;
				arr_index = 0;
			}
			else if( retStr.compareTo("td") == 0 || retStr.compareTo("th") == 0)
			{
				if ( loop_index == qfield)
				{
					if ( retStr.compareTo("td") == 0)
						tmpStr = getContent("td");
					else
						tmpStr = getContent("th");

					tmpStr = tmpStr.replaceAll("<[^<>]+>", "").trim();
					arr_index++;
				}
				loop_index++;
			}
			nextTag();
			retStr = getTagName();
		}

		if ( arr_index > 1)
			adapter.update(show_count-1, 2, tmpStr);

		return true;
	}

	//	public boolean parse(Grade_Adapter adapter)
	//	{
	//		String retStr;
	//		String tmpStr;
	//		int loop_index = 0;
	//		int arr_index = 0;
	//		int show_count = 10;
	//
	//		if ( !checkTable())
	//			return false;
	//
	//		nextTag();
	//		retStr = getTagName();
	//
	//		while( retStr.compareTo("/table") != 0 && show_count >= 0)
	//		{
	//			if ( retStr.compareTo("tr") == 0)
	//			{				
	//				if ( arr_index > 0)
	//				{
	//					adapter.add(items);
	//					show_count--;
	//				}
	//				loop_index = 0;
	//				arr_index = 0;
	//			}
	//			else if( retStr.compareTo("td") == 0 || retStr.compareTo("th") == 0)
	//			{
	//				if ( loop_index <= 1 || loop_index == qfield)
	//				{
	//					if ( retStr.compareTo("td") == 0)
	//						tmpStr = getContent("td");
	//					else
	//						tmpStr = getContent("th");
	//
	//					tmpStr = tmpStr.replaceAll("<[^<>]+>", "").trim();
	//					items[arr_index++] = tmpStr;
	//				}
	//				loop_index++;
	//			}
	//			nextTag();
	//			retStr = getTagName();
	//		}
	//
	//		if ( arr_index > 0)
	//		{
	//			adapter.add(items);
	//		}
	//
	//		return true;
	//	}

}
